<?php
namespace app\admin\controller;

use controller\BasicAdmin;
use think\Db;

/**
 * api接口
 * Class Api
 * @package app\admin\controller
 * @author ma <80234043@qq.com>
 * @date 2022/02/12 18:12
 */
class Api extends BasicAdmin
{
    //按年聚合集团所有子公司数据
    public function getGroupCompanyTotalTax(){
        $year = $this->request->post('year'); 
        $country = $this->request->post('country'); 
        $group_company_id = $this->request->post('group_company_id/d');
        //查询条件
        $map['a.is_deleted'] = 0;
        //国家 
        if(!empty($country)){
            $map['a.country'] = $country;
        }
        //集团公司id
        if($group_company_id>0){
            $map['a.group_company_id'] = $group_company_id;
        }
        //年份
        if($year>0){
            $map['a.year'] = $year;
        }

        $list = Db::table('system_intertax')
        ->alias('a')
        ->join('system_group_company b','a.group_company_id=b.id','left')
        ->where($map)
        ->group('a.group_company_id')
        ->field('sum(a.total_income) as year_totla_income,b.name,a.year')
        ->select();

        return json(['data'=>$list,'code'=>0,'message'=>'ok']);
    }

    //查询子公司某年的数据，并返回上一年的数据
    public function getSubsidiaryYearData(){
        $year = $this->request->post('year'); 
        $subsidiary_id = $this->request->post('subsidiary_id/d');
        //查询条件
        $map['a.is_deleted'] = 1;
        //年份
        if($year>0){
            $map['a.year'] = ['IN',[$year-1,$year]];
        }
        //子公司id
        if($subsidiary_id>0){
            $map['a.subsidiary_id'] = $subsidiary_id;
        }

        $list = Db::table('system_intertax')
        ->alias('a')
        ->join('system_subsidiary b','a.subsidiary_id=b.id','left')
        ->where($map)
        ->field('a.*,b.subsidiary')
        ->select();
        return json(['data'=>$list,'code'=>0,'message'=>'ok']);
        
    }

    //根据集团公司id获取子公司信息
    public function getSubsidiarys(){
        //$group_company_id = $this->request->post('group_company_id');
        $group_company_id = 1;
        $subsidiarys = Db::name('SystemSubsidiary')->where(['group_company_id'=>$group_company_id])->column('subsidiary','id');
        return json(['data'=>$subsidiarys,'code'=>0,'message'=>'ok']);
    }
}